﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using KingsSharp.Core;
using System.Reflection;

namespace KingsSharp.Core.Data.Entity.Extensions
{
    /// <summary>
    /// 业务实现基类扩展
    /// </summary>
    public static class ServiceBaseExtension
    {
        /// <summary>
        /// 设置链接字符串
        /// </summary>
        /// <param name="Sbase">业务基类</param>
        /// <param name="ConnectionString">连接字符串</param>
        /// <returns></returns>
        public static ServiceBase SetConnectionString(this ServiceBase Sbase, string ConnectionString)
        {
            BindingFlags flag = BindingFlags.Instance | BindingFlags.NonPublic;
            Type type = typeof(ServiceBase);
            PropertyInfo field = type.GetProperty("UnitOfWork", flag);
            var DbContext = (AbstractDbContext)field.GetValue(Sbase, null);
            DbContext.Database.Connection.ConnectionString = ConnectionString;
            return Sbase;
        }
        /// <summary>
        /// 设置操作单元的链接字符串
        /// </summary>
        /// <typeparam name="T">要设置的类型</typeparam>
        /// <param name="Sources">设置的对象</param>
        /// <param name="ConnectionStrng">连接字符串</param>
        /// <returns>设置的对象</returns>
        public static T SetConnectionString<T>(this T Sources, string ConnectionStrng)
        {
            object sources = Sources;

            try
            {
                ((ServiceBase)sources).SetConnectionString(ConnectionStrng);//设置。
            }
            catch (Exception e)
            {
                throw e;
            }
            return Sources;
        }
    }
}
